package com.sxx.beautifulcity.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.baidu.platform.comapi.map.I;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.database.LoginUserHelper;
import com.sxx.beautifulcity.database.UserHelper;
import com.sxx.beautifulcity.entity.greendao.LoginUserDaoEntity;
import com.sxx.beautifulcity.fragment.HomeFragment;
import com.sxx.beautifulcity.fragment.KeepHealthFragment;
import com.sxx.beautifulcity.fragment.LocationFragment;
import com.sxx.beautifulcity.fragment.NearFragment;
import com.sxx.beautifulcity.fragment.SearchFragment;

import java.io.File;
import java.util.List;


public class MainActivity extends FragmentActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private Handler handler;

    private FragmentManager fm;
    private TabHost tabHost;
    private TextView city_namemainTv, submit_head_image, user_nameTv;
    private ImageView head_image;
    private String cityname;
   private LoginUserHelper helper;
//    Message message = new Message();
    // private UserHelper userHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        initView();
        createTabSpec();
        initFragment();
    }

    /****
     * 初始化组件
     */
    public void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        helper = new LoginUserHelper();
        // userHelper = new UserHelper();

        city_namemainTv = (TextView) findViewById(R.id.city_namemainTv);
        final Intent intent = getIntent();
        cityname = intent.getStringExtra("lngCityName");
        city_namemainTv.setText(cityname);

        submit_head_image = (TextView) findViewById(R.id.submit_head_image);
        submit_head_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this, SelectImageActivity.class);
                startActivity(intent1);
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
            }
        });

        head_image = (ImageView) findViewById(R.id.head_image);

        File picture = new File(Environment.getExternalStorageDirectory(), "temp_head_image.jpg");
        if (picture.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/temp_head_image.jpg");
            head_image.setImageBitmap(bitmap);
        }

        user_nameTv = (TextView) findViewById(R.id.user_nameTv);
//        Intent intent1 = getIntent();
//        String name = intent1.getStringExtra("name");
//        if (helper != null) {
//            helper.deleteAll();
//        }else{

        user_nameTv.setText(helper.queryAll().get(0).getUser_login_name());
//            Log.i("jnjd" , user_nameTv + "");
//       }


//        handler = new Handler() {
//            public void handleMessage(Message message) {
//                if (message.what == 0) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                    MainActivity.this.finish();
//                } else if (message.what == 1) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                } else if (message.what == 2) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                } else if (message.what == 3) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                } else if (message.what == 4) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                } else if (message.what == 5) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                } else if (message.what == 6) {
//
//                    overridePendingTransition(R.anim.left_into, R.anim.right_out);
//                }
//            }
//        };


    }

    /****
     * 创建TabSpec
     **/
    public void createTabSpec() {
        //创建首页
        tabHost.setup();
        TabHost.TabSpec home = tabHost.newTabSpec("home");
        View homeView = getLayoutInflater().inflate(R.layout.table_home_activity, null);
        home.setIndicator(homeView);
        //Intent homeintent = new Intent();
        // homeintent.setClass(this,HomeFragment.class);
        home.setContent(R.id.homefragment);
        tabHost.addTab(home);

        //创建养生
        TabHost.TabSpec keep_health = tabHost.newTabSpec("keep_health");
        View keepView = getLayoutInflater().inflate(R.layout.table_weather_activity, null);
        keep_health.setIndicator(keepView);
        keep_health.setContent(R.id.keep_health);
        tabHost.addTab(keep_health);

        //创建附近
        TabHost.TabSpec near = tabHost.newTabSpec("near");
        View nearView = getLayoutInflater().inflate(R.layout.table_near_activity, null);
        near.setIndicator(nearView);
        near.setContent(R.id.nearfragment);
        tabHost.addTab(near);
        //创建搜索
        TabHost.TabSpec search = tabHost.newTabSpec("search");
        View searchView = getLayoutInflater().inflate(R.layout.table_search_activity, null);
        search.setIndicator(searchView);
        search.setContent(R.id.searchfragment);
        tabHost.addTab(search);
        //导航
        TabHost.TabSpec localization = tabHost.newTabSpec("localization");
        View locationView = getLayoutInflater().inflate(R.layout.table_fragment_location, null);
        localization.setIndicator(locationView);
        localization.setContent(R.id.GPSFragment);
        tabHost.addTab(localization);
    }

    /**
     * 替换布局
     **/
    public void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.homefragment, new HomeFragment());
        ft.replace(R.id.nearfragment, new NearFragment());
        ft.replace(R.id.GPSFragment, new LocationFragment());
        ft.replace(R.id.searchfragment, new SearchFragment());
        ft.replace(R.id.keep_health, new KeepHealthFragment());
        ft.commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.login) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.register) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, RegisterActivity.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.myCollection) {
            Intent intent1 = new Intent();
            intent1.setClass(MainActivity.this, StoreActivity.class);
            startActivity(intent1);
            finish();

        } else if (id == R.id.selectCity) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ActivitySelectedActivity.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.setSoft) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, SetSoftActivity.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.baoutsoftware) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, AboutSoftActivity.class);
            startActivity(intent);
            finish();

        } else if (id == R.id.recommendSoft) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, RecommendSoftActivity.class);
            startActivity(intent);
            finish();
//            message.what = 6;
//            handler.sendMessageDelayed(message, 1000);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
